package com.bigdata.appliance.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.appliance.common.exception.BusinessException;
import com.bigdata.appliance.common.exception.ResponseCode;
import com.bigdata.appliance.entity.Address;
import com.bigdata.appliance.entity.User;
import com.bigdata.appliance.mapper.AddressMapper;
import com.bigdata.appliance.mapper.UserMapper;
import com.bigdata.appliance.service.IAddressService;
import com.bigdata.appliance.vo.req.AddAddressReqVO;
import com.bigdata.appliance.vo.req.EditAddressReqVO;
import com.bigdata.appliance.vo.req.GetUserAddressListReqVO;
import com.bigdata.appliance.vo.req.SetDefaultAddressReqVO;
import com.bigdata.appliance.vo.resp.GetUserAddressLIstRespVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author appliance-shop
 * @since 2023-12-23
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private UserMapper userMapper;

    @Transactional
    @Override
    public void addAddress(AddAddressReqVO vo) {
        Address address = new Address();
        address.setUserId(vo.getUserId());
        address.setName(vo.getName());
        address.setPhone(vo.getPhone());
        address.setAddress(vo.getAddress());
        int result = addressMapper.insert(address);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
        if (vo.getIsDefault() == 1) {
            User user = userMapper.selectById(vo.getUserId());
            user.setDefaultAddressId(address.getId());
            int result1 = userMapper.updateById(user);
            if (result1 != 1) {
                throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
            }
        }
    }

    @Transactional
    @Override
    public void editAddress(EditAddressReqVO vo) {
        Address address = addressMapper.selectById(vo.getId());
        address.setName(vo.getName());
        address.setPhone(vo.getPhone());
        address.setAddress(vo.getAddress());
        int result = addressMapper.updateById(address);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
        if (vo.getIsDefault() == 1) {
            User user = userMapper.selectById(address.getUserId());
            user.setDefaultAddressId(address.getId());
            int result1 = userMapper.updateById(user);
            if (result1 != 1) {
                throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
            }
        }
    }

    @Override
    public void setDefaultAddress(SetDefaultAddressReqVO vo) {
        Address address = addressMapper.selectById(vo.getId());
        User user = userMapper.selectById(address.getUserId());
        user.setDefaultAddressId(address.getId());
        int result = userMapper.updateById(user);
        if (result != 1) {
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(), ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public GetUserAddressLIstRespVO getUserAddress(GetUserAddressListReqVO vo) {
        List<Address> addressList = query().select("*").eq("user_id", vo.getUserId()).list();
        User user = userMapper.selectById(vo.getUserId());
        GetUserAddressLIstRespVO respVO = new GetUserAddressLIstRespVO();
        respVO.setAddressList(addressList);
        respVO.setDefaultId(user.getDefaultAddressId());
        return respVO;
    }
}
